/*
 *所有关于EQU_COM_DEFINITION_TASK类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*EQU_COM_DEFINITION_TASKService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.System.IRepositories;
using System;

namespace VOL.System.Services
{
    public partial class EQU_COM_DEFINITION_TASKService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IEQU_COM_DEFINITION_TASKRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public EQU_COM_DEFINITION_TASKService(
            IEQU_COM_DEFINITION_TASKRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override PageGridData<EQU_COM_DEFINITION_TASK> GetPageData(PageDataOptions options)
        {
            QuerySql = $@"SELECT [ComDefintion_TaskId],p.IsActivation as IsActivation
      ,[ComDefintionTaskNameZH]
      ,[TaskNo]
      ,[MovePath_Id]
      ,[MovePathSub_Id]
      ,[Command]
      ,[Equipment_Id]
      ,[Source]
      ,[IpUrl]
      ,t.[ExecuteStart]
      ,t.[ExecuteEnd]
      ,[SubPathNo]
      ,[TaskType]
      ,[EQUTask_Id]
      ,[OperationName]
      ,t.[OrderCode]
      ,t.[Description]
      ,t.[Status]
      ,[SHELVES_Id]
      ,[ActionEquipmentId]
      ,[ActionSubPathNo]
      ,[ActionSort]
      ,[ProgramId]
      ,[ToolId]
      ,[ExecuteTaskDiffTime]
      ,[SecornSeq]
      ,[time]
      ,t.[ProcessingNumber]
      ,[IsCurrentOrderCode]      
      ,t.[CreateBy]
      ,t.[CreateOn]
      ,t.[UpdateBy]
      ,t.[LastUpdateOn]
      ,t.[IsDeleted]
  FROM [dbo].[EQU_COM_DEFINITION_TASK] t
  left join PLAN_WORK_ORDER p on t.OrderCode=p.OrderCode";
            return base.GetPageData(options);
        }
        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            foreach (var key in keys)
            {
                var eQU_COM_DEFINITION_TASK = _repository.DbContext.Set<EQU_COM_DEFINITION_TASK>().Where(t => t.ComDefintion_TaskId == Guid.Parse(key.ToString().ToUpper())).FirstOrDefault();
                if (eQU_COM_DEFINITION_TASK != null&& eQU_COM_DEFINITION_TASK.ToolId!=null)
                {
                   var pROC_PROGRAM=  _repository.DbContext.Set<PROC_PROGRAM>().Where(t => t.Id == Guid.Parse(eQU_COM_DEFINITION_TASK.ProgramId.ToString().ToUpper())).FirstOrDefault();
                    var eQU_TOOL = _repository.DbContext.Set<EQU_TOOL>().Where(t => t.Id == Guid.Parse(eQU_COM_DEFINITION_TASK.ToolId.ToString().ToUpper())).FirstOrDefault();
                    EQU_TOOL_PROGAME eQU_TOOL_PROGAME = _repository.DbContext.Set<EQU_TOOL_PROGAME>().Where(t => t.ToolId == eQU_TOOL.Id.ToString().ToUpper()&&t.ProgrameId==pROC_PROGRAM.Id.ToString().ToUpper()).FirstOrDefault();
                   
                    if (eQU_TOOL_PROGAME!=null)
                    {
                        _repository.DbContext.Set<EQU_TOOL_PROGAME>().Remove(eQU_TOOL_PROGAME);
                        _repository.DbContext.SaveChanges();


                    }
                }
            }
           
            return base.Del(keys, delList);
        }
    }
}
